import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

export 'package:pull_to_refresh/pull_to_refresh.dart';

/// 定义创建controller回调,提供可以自定义的控制下啦效果
typedef _OnControllerCreateCallback(RefreshController _refreshController);

/// 定义下啦刷新的回掉,要求返回对应的是否成功bool
typedef Future<ActionStatus> _OnDataRefresh();

/// 定义上拉加载的回掉,要求返回对应的是否成功bool，对应的null直接表达为空数据，true表示成功 false失败了
typedef Future<ActionStatus> _OnDataLoadingMore();

/// 响应状态给到下啦刷新控制状态
enum ActionStatus {
  /// 成功
  Success,

  /// 失败
  Failed,

  /// 数据为空
  Empty
}

class PullRefresh extends StatefulWidget {
  /// 子类控件
  final Widget child;

  /// 自定义的头部header，传递会替换全局的，必须要求是sliver的组件,有需要可以参考app里面配置全局的组件进行继承父类实现
  final Widget? header;

  /// 自定义的尾部header，传递会替换全局的，必须要求是sliver的组件,有需要可以参考app里面配置全局的组件进行继承父类实现
  final Widget? footer;

  /// 是否开启下啦刷新
  final bool enablePullRefresh;

  /// 下啦刷新回调
  final _OnDataRefresh? onRefresh;

  /// 是否开启上拉加载
  final bool enableLoadMore;

  /// 上啦加载回调
  final _OnDataLoadingMore? onLoading;

  /// 是否一进入列表就展示下啦刷新效果
  final bool initialRefresh;

  /// 创建controller回掉controller
  final _OnControllerCreateCallback? onControllerCreateCallback;

  const PullRefresh({
    Key? key,
    required this.child,
    this.header,
    this.footer,
    this.enablePullRefresh = false,
    this.enableLoadMore = false,
    this.initialRefresh = false,
    this.onControllerCreateCallback,
    this.onRefresh,
    this.onLoading,
  }) : super(key: key);

  @override
  _PullRefreshState createState() => _PullRefreshState();
}

class _PullRefreshState extends State<PullRefresh> {
  late final RefreshController _refreshController;

  @override
  void initState() {
    super.initState();
    _refreshController = RefreshController(initialRefresh: widget.initialRefresh);
    widget.onControllerCreateCallback?.call(_refreshController);
  }

  @override
  void dispose() {
    _refreshController.dispose();
    super.dispose();
  }

  /// 下啦刷新数据
  void _onRefreshData() async {
    if (widget.onRefresh == null) {
      return;
    }
    final ActionStatus actionStatus = await widget.onRefresh!.call();
    assert(actionStatus != ActionStatus.Empty);
    switch (actionStatus) {
      case ActionStatus.Success:
        _refreshController.refreshCompleted();
        break;
      case ActionStatus.Failed:
        _refreshController.refreshFailed();
        break;
      case ActionStatus.Empty:
        break;
    }
  }

  /// 上拉加载
  void _onLoadingMore() async {
    if (widget.onLoading == null) {
      return;
    }
    final ActionStatus actionStatus = await widget.onLoading!.call();
    switch (actionStatus) {
      case ActionStatus.Success:
        _refreshController.loadComplete();
        break;
      case ActionStatus.Failed:
        _refreshController.loadFailed();
        break;
      case ActionStatus.Empty:
        _refreshController.loadNoData();
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      dragStartBehavior: DragStartBehavior.down,
      controller: _refreshController,
      enablePullDown: widget.enablePullRefresh,
      enablePullUp: widget.enableLoadMore,
      onRefresh: _onRefreshData,
      onLoading: _onLoadingMore,
      header: widget.header,
      footer: widget.footer,
      child: widget.child,
    );
  }
}
